﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Templates;

namespace RibbonEditor.Controls.Templates
{
    public partial class OverflowAreaControl : UserControl, IControlRulable
    {
        private OverflowArea innerOverflowArea;

        public OverflowArea InnerControlRef
        {
            get { return innerOverflowArea; }
            set { innerOverflowArea = value; }
        }

        public OverflowAreaControl()
        {
            InitializeComponent();

            this.ecDisplayMode.Values = Enum.GetValues(typeof(Enumerations.DisplayMode));

            this.innerOverflowArea = new OverflowArea();
            this.FillControls();
        }

        public OverflowAreaControl(OverflowArea overflowArea)
        {
            this.innerOverflowArea = overflowArea;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.ecDisplayMode.SelectedValue = this.innerOverflowArea.DisplayMode.Value.ToString();
                this.strTemplateAlias.Value = this.innerOverflowArea.TemplateAlias;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerOverflowArea.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), this.ecDisplayMode.SelectedValue);
                    this.innerOverflowArea.TemplateAlias = this.strTemplateAlias.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ControlRefControl_Leave(object sender, EventArgs e)
        {
            try
            {
                bool isDirty = this.ecDisplayMode.IsDirty || this.strTemplateAlias.IsDirty;

                if (isDirty)
                {
                    if (DialogResult.Yes == MessageBox.Show(this, "You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        this.Save();
                        this.ecDisplayMode.IsDirty = false;
                        this.strTemplateAlias.IsDirty = false;
                    }
                    else
                    {
                        this.ecDisplayMode.ReinitValues();
                        this.strTemplateAlias.ReinitValues();
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerOverflowArea.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
